 
import { cache_key_arg0, cache_return } from "utils/decorate/decorate"
import { roomCostStrategy, strcuture_radio } from "./BaseCostStrategy"




export function setArmyBlock (cost:CostMatrix,x:number,y:number,val:number)
{
    cost.set(x, y, val)
    if (x > 2) {
        cost.set(x - 1, y, val)
    }
    if (y > 2) {
        cost.set(x, y - 1, val)
    }
    if (x > 2 && y > 2) {
        cost.set(x - 1, y - 1, val)
    }
}
export class roomCostStrategyOfTeam4 extends roomCostStrategy
{
    @cache_return("cache",0,cache_key_arg0)
    public getCost(roomName:string):CostMatrix
    {
        const room = Game.rooms[roomName]

        const tmpcosts  = new PathFinder.CostMatrix();
        const terrian = new Room.Terrain(roomName)
        /* 第一层设置沼泽 */
        for (let x = 0; x < 50; x++)
        {
            for (let y = 0; y < 50; y++)
            {
                if (terrian.get(x, y) == TERRAIN_MASK_SWAMP)
                {
                     setArmyBlock(tmpcosts,x,y,40);
                }
                else if (terrian.get(x, y) == TERRAIN_MASK_WALL)
                {
                    setArmyBlock(tmpcosts,x,y,0xff);
                }
            }
        }
        if (!room)
        {
            // 没有视野使用缓存
            const station = global.world.PollStructure(roomName);
            if(station)
            {
                station.cacheData.forEach(struct=>{
                        if (struct.structureType !== STRUCTURE_CONTAINER &&
                        !(struct.structureType == STRUCTURE_RAMPART && struct.my)
                    )
                    {

                        setArmyBlock(tmpcosts,struct.pos.x,struct.pos.y, 0xff);
                    }
                })
            }
            return tmpcosts;
        }
        // 不在我的房间.
    
        room.find(FIND_STRUCTURES).forEach(struct => {
                if (struct.structureType !== STRUCTURE_CONTAINER &&
                !(struct.structureType == STRUCTURE_RAMPART && struct.my)
            )
            {
                setArmyBlock(tmpcosts,struct.pos.x,struct.pos.y, 0xff);
            }
        })
        if(room.IsMy())
        {
            room.find(FIND_CONSTRUCTION_SITES).forEach(struct => {
                if (struct.structureType === STRUCTURE_ROAD) {
                    tmpcosts.set(struct.pos.x, struct.pos.y, 1)
                }
                // 不能穿过无法行走的建筑
                else if (struct.structureType !== STRUCTURE_CONTAINER &&
                    (struct.structureType !== STRUCTURE_RAMPART || !struct.my)
                )
                {
                    setArmyBlock(tmpcosts,struct.pos.x,struct.pos.y, 0xff);
                }
            })
        }


        return tmpcosts;
    }

    private setBlockStructure(room:Room,costs: CostMatrix)
    {
        room.find(FIND_STRUCTURES).forEach(struct => {
            if (struct.structureType !== STRUCTURE_CONTAINER &&
            !(struct.structureType == STRUCTURE_RAMPART && struct.my)
        )
        {
            setArmyBlock(costs,struct.pos.x,struct.pos.y, 0xff);
        }
    })
    }

    private setDestoryStructure(room:Room,costs: CostMatrix)
    {
        room.find(FIND_STRUCTURES).forEach(struct => {
            if (struct.structureType === STRUCTURE_ROAD) {
                if(costs.get(struct.pos.x, struct.pos.y)<=1)
                {
                    costs.set(struct.pos.x, struct.pos.y, 1)
                }
                
            }
            // 不能穿过无法行走的建筑
            else  
            {
                
                {
                    const cost = Math.round( struct.hits*strcuture_radio )+5;
                    if(costs.get(struct.pos.x, struct.pos.y)< cost)
                    {
                        costs.set(struct.pos.x, struct.pos.y, cost)
                    }
                }
                
            }
            
        })
    }
}
